package com.codingfuture.glaciercollect.quart;


import com.codingfuture.glaciercollect.listener.MqttListen;
import com.codingfuture.glaciercommon.util.System;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @Author: Sun
 * @DateTime: 2025/4/1 16:00
 * @Description:随机生成一个温湿度任务
 **/
@Component
public class WarmHumidJob implements Job {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
@Autowired
private MqttListen mqttListen;

    @Autowired
    private MqttClientTemplate client;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {


        //向仪表要温度
        client.publish("/topic/publish_temperature", ("" +
                "{\n" +
                "        \"messageid\": \"3FBD8A5D-9824-D355-EF7A-B31A609522CC\",\n" +
                "        \"param\": {\n" +
                "            \"type\": \"info\"\n" +
                "        }\n" +
                "}").getBytes(StandardCharsets.UTF_8));

        System.green.println("adasd11111");
        mqttListen.sub();

    }
}



//        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
//        int baseTemperature = jobDataMap.getInt("warmDate");
//        int baseHumidity = jobDataMap.getInt("humidDate");
//
//        int temperature = random.nextInt(11) + baseTemperature - 5;
//        int humidity = random.nextInt(11) + baseHumidity - 5;
//        THDateDTO result = new THDateDTO();
//        result.setHum(humidity);
//        result.setTem(temperature);
//
//
//        String groupName = context.getJobDetail().getKey().getGroup();
//        WarmHumidQuartz.storeGroupResult(groupName, result);
//        System.err.println(result);
//
//        // 获取组名作为 redis 的 key
//        String sn = context.getJobDetail().getKey().getGroup();
//        System.err.println(sn);
//
////         更新 redis 的温湿度
//        BoundHashOperations<String, Object, Object> ops = stringRedisTemplate.boundHashOps(sn);
//        ops.put("hum", String.valueOf(humidity));
//        ops.put("tem", String.valueOf(temperature));
//        //发布消息
//        String jsonResult = JSON.toJSONString(result);//把结果类转化为JSON发布出去
//        redisTemplate.convertAndSend(sn, jsonResult);

//    /**
//     * 发布
//     *
//     * @return
//     */
//    public boolean publish() {
//        client.publish("/topic/publish_temperature", ("" +
//                "{\n" +
//                "        \"messageid\": \"3FBD8A5D-9824-D355-EF7A-B31A609522CC\",\n" +
//                "        \"param\": {\n" +
//                "            \"type\": \"info\"\n" +
//                "        }\n" +
//                "}").getBytes(StandardCharsets.UTF_8));
//        return true;
//    }
//
//    /**
//     * 订阅
//     *
//     * @return
//     */
//    public boolean sub() {
//        client.subQos0("/topic/subscribe_temperature", (context, topic, message, payload) -> {
//            String s = new String(payload, StandardCharsets.UTF_8);
//            logger.info(topic + '\t' + s);
//            //对象->db
//        });
//        return true;
//    }